package com.puman.beautyfarmcounselor.ui.customer.adpter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.puman.beautyfarmcounselor.MainActivity;
import com.puman.beautyfarmcounselor.R;
import com.puman.beautyfarmcounselor.ui.customer.CusLableFragment;
import com.puman.beautyfarmcounselor.ui.customer.model.LableData;
import com.puman.beautyfarmcounselor.ui.customer.model.LableInfo;
import com.puman.beautyfarmcounselor.util.MethedUtil;

import java.util.ArrayList;
import java.util.List;


@SuppressLint("NewApi")
public class LableGridviewAdapter extends BaseAdapter {

    private LayoutInflater listContainer;
    private int position = 0;
    private int flag = 1;
    CusLableFragment cusLableFragment;

    public final class ListItemView {
        public TextView labelText;
        public ImageView deleteIcon;
        public RelativeLayout itemLayout;
    }

    public LableGridviewAdapter(CusLableFragment cusLableFragment, int flag) {//flag 1:cus  2:lib
        listContainer = LayoutInflater.from(MainActivity.getInstace());
        this.cusLableFragment = cusLableFragment;
        this.flag = flag;
    }

    public int getCount() {
        if (flag == 1)
            return cusLableFragment.cusLableList.size();
        else
            return cusLableFragment.libLableList.size();
    }

    public Object getItem(int arg0) {
        return null;
    }

    public long getItemId(int arg0) {
        return 0;
    }

    /**
     *
     */
    public View getView(final int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final ListItemView listItemView;
        if (convertView == null) {
            listItemView = new ListItemView();
            convertView = listContainer.inflate(R.layout.lable_item_layout,
                    null);
            listItemView.labelText = (TextView) convertView.findViewById(R.id.label_text);
            listItemView.deleteIcon = (ImageView) convertView.findViewById(R.id.label_opertation);
            listItemView.itemLayout = (RelativeLayout) convertView.findViewById(R.id.item_layout);
            convertView.setTag(listItemView);
        } else {
            listItemView = (ListItemView) convertView.getTag();
        }
        listItemView.deleteIcon.setTag(position);
        if (flag == 1) {
            setDeleteIcon(listItemView);
            listItemView.labelText.setText(cusLableFragment.cusLableList.get(position).getName());
        } else {
            MethedUtil.UtilLog("flag:2");
            setBack(listItemView);
            listItemView.labelText.setText(cusLableFragment.libLableList.get(position).getName());
        }

        return convertView;
    }

    private void setDeleteIcon(ListItemView listItemView) {
        if (cusLableFragment.cusLableList.get(position).isSelect()) {
            listItemView.deleteIcon.setVisibility(View.VISIBLE);
            listItemView.deleteIcon.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    int positon = (int) v.getTag();
                    for (int i = 0; i < cusLableFragment.libLableList.size(); i++) {
                        if (cusLableFragment.libLableList.get(i).getName().equals(cusLableFragment.cusLableList.get(positon).getName())) {
                            cusLableFragment.libLableList.get(i).setIsSelect(false);
                            cusLableFragment.libAdapter.notifyDataSetChanged();
                            cusLableFragment.modifyLayout();
                        }
                    }
                    cusLableFragment.cusLableList.remove(positon);
                    cusLableFragment.cusAdapter.notifyDataSetChanged();
                    cusLableFragment.libAdapter.notifyDataSetChanged();
                }
            });
        } else {
            listItemView.deleteIcon.setVisibility(View.GONE);
        }
    }

    private void setBack(ListItemView listItemView) {

        int pos = (int) listItemView.deleteIcon.getTag();
        MethedUtil.UtilLog("flag:"+cusLableFragment.libLableList.get(pos).isSelect());
        MethedUtil.UtilLog("*i:"+pos+",flag:"+cusLableFragment.libLableList.get(pos).isSelect());
        if (cusLableFragment.libLableList.get(pos).isSelect()) {
            listItemView.itemLayout.setBackground(MainActivity.getInstace().getResources().getDrawable(R.drawable.circle_white_bg_theme_bound));
        } else {
            listItemView.itemLayout.setBackground(MainActivity.getInstace().getResources().getDrawable(R.drawable.circle_white_bg_gray_bound));
        }
    }


}
